package com.zyx.kafkademo.utils;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author zyx
 * @since 2021/5/29 08:50
 * desc: Watersensor生产者案例
 */
public class WaterSensorDemo {
    public static void main(String[] args) throws InterruptedException {
        Random random = new Random();
        String id = "ws_00" + random.nextInt(10);
        String ts = String.valueOf(System.currentTimeMillis());
        String vc = String.valueOf(random.nextInt(100));
        KafkaProducer<String, String> kafkaProdocuer = KafkaUtils.getKafkaProdocuer();
        for (int i = 0; i < 10000; i++) {
            String record = id + "," + ts + "," + vc;
            ProducerRecord<String, String> producerRecord = new ProducerRecord<>("sensor_source", record);
            kafkaProdocuer.send(producerRecord);
            TimeUnit.MILLISECONDS.sleep(30);
        }
    }
}
